package G1;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOC {
	private String path;
    private String number;
    private String title;
    private String author;
    private String biblio;
    private String text;

    public DOC(){
    }
    
    public DOC(File file) throws IOException, ParserConfigurationException, SAXException{
    	DocumentBuilder _docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document _doc = _docBuilder.parse( file );
		NodeList _rowlist = _doc.getElementsByTagName("DOC");
		Node _row = _rowlist.item( 0 );
		setPath(file.getPath());
		if ( _row.getNodeType() == Node.ELEMENT_NODE ) {
			NodeList _child = _row.getChildNodes();
			for ( int j = 0; j < _child.getLength(); j++ ) {
				Node _ele = _child.item( j );
				if ( _ele.getNodeType() == Node.ELEMENT_NODE ) {
					String _name = _ele.getNodeName();
					String _content = _ele.getTextContent();
					if ( _name.equalsIgnoreCase( "DOCNO" ) ) {
						setNumber( _content.replace("\n", " ")  );
					} else if ( _name.equalsIgnoreCase( "TITLE" ) ) {
						setTitle( _content.replace("\n", " ") );
					} else if ( _name.equalsIgnoreCase( "AUTHOR" ) ) {
						setAuthor( _content.replace("\n", " ") );
					} else if ( _name.equalsIgnoreCase( "BIBLIO" )) {
						setBiblio( _content.replace("\n", " ") );
					} else if ( _name.equalsIgnoreCase( "TEXT" ) ) {
						setText( _content.replace("\n", " ") );
					} else {
						throw new SAXException( "Unexpacted Element: " + _name );
					}
				}
			}
		}
		
	}
    
    public String toString()
    {
        return number+"\n"+title+"\n"+author+"\n"+biblio+"\n"+text;
    }

    
    public String getTitle()
    {
        return title;
    }

    public void setTitle( String title )
    {
        this.title = title;
    }

    
    public String getNumber()
    {
        return number;
    }

    public void setNumber( String number )
    {
        this.number = number;
    }

    
    public String getAuthor()
    {
        return author;
    }

    public void setAuthor( String author )
    {
        this.author = author;
    }

    
    public String getBiblio()
    {
        return biblio;
    }

    public void setBiblio( String biblio )
    {
        this.biblio = biblio;
    }

    
    public String getText()
    {
        return text;
    }

    public void setText( String text )
    {
        this.text = text;
    }
	
	
	public String getPath() {
		return path;
	}
	
	public String getFileName() {
		return path.substring(path.lastIndexOf('\\')+1);
	}
	public void setPath(String path) {
		this.path = path;
	}
}
